//1.添加material依赖
import 'package:flutter/material.dart';
import 'package:power_house/util/common_toast.dart';
import 'package:power_house/util/dio_http.dart';
import 'package:power_house/util/string_util.dart';

import '../config.dart';

//2.编写无状态组件
class RegisterPage extends StatefulWidget {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var repeatPasswordController = TextEditingController();

  _registerHandler() async {
    var username = usernameController.text;
    var password = passwordController.text;
    var repeatPassword = repeatPasswordController.text;

    if (stringIsNullOrEmpty(username)) {
      CommonToast.showToast('用户名不能为空');
    }
    if (stringIsNullOrEmpty(password)) {
      CommonToast.showToast('密码不能为空');
    }
    if (stringIsNullOrEmpty(repeatPassword)) {
      CommonToast.showToast('请确认密码');
    }
    if (password != repeatPassword) {
      CommonToast.showToast('两次密码不一致');
    }

    const url = '${Config.baseUrl}/register';
    var params = {
      "username": username,
      "password": password,
    };
    var response = await DioHttp.of(context).post(url, params);

    var resp = response.data;
    if (resp == null) {
      return;
    }
    // 拦截到错误, 弹框提示
    if (resp['code'] != 200) {
      CommonToast.showToast(resp['data']['message'] ?? '内部错误');
      return; // 不继续执行流程
    }

    if (resp['data']['isSuccess'] == true) {
      if (context.mounted) {
        Navigator.of(context).pushReplacementNamed('login');
        CommonToast.showToast('注册成功');
      }
    } else {
      CommonToast.showToast('注册失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('注册'),
      ),
      body: SafeArea(
        minimum: const EdgeInsets.all(30),
        child: ListView(
          children: <Widget>[
            const Padding(padding: EdgeInsets.all(10)),
            TextField(
              controller: usernameController,
              decoration: const InputDecoration(
                labelText: '用户名',
                hintText: '请输入用户名',
              ),
            ),
            const Padding(padding: EdgeInsets.all(10)), //padding
            TextField(
              controller: passwordController,
              obscureText: true,
              decoration: const InputDecoration(
                labelText: '密码',
                hintText: '请输入密码',
              ),
            ),
            const Padding(padding: EdgeInsets.all(10)), //padding
            TextField(
              obscureText: true,
              controller: repeatPasswordController,
              decoration: const InputDecoration(
                labelText: '确认密码',
                hintText: '请确认密码',
              ),
            ),
            const Padding(padding: EdgeInsets.all(10)), //padding
            ElevatedButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                ),
                onPressed: () {
                  _registerHandler();
                },
                child: const Text(
                  '注册',
                  style: TextStyle(color: Colors.white),
                )),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text('已有账号'),
                TextButton(
                    onPressed: () {
                      Navigator.pushReplacementNamed(context, "/login");
                    },
                    child: const Text(
                      '去登录~',
                      style: TextStyle(color: Colors.green),
                    ))
              ],
            )
          ],
        ),
      ),
    );
  }
}
